<?php
namespace classlib\acl;
use \classlib\core\model;
class session extends acl_abstract {
	/**
	 * 构造初始化预设信息
	 * @param array $args
	 */
	public function __construct($args) {
		parent::__construct($args);
		if (!session_id()) {
			if (isset($args['path']) && is_dir($args['path'])) {
				session_save_path($args['path']);
			}
			session_start();
		}
	}
	
	/**
	 * 程序页面验证是否登录
	 * {@inheritDoc}
	 * @see \classlib\acl\acl_abstract::islogin()
	 */
	public function islogin($sign='') {
		if (!isset($_SESSION['user']) || !isset($_SESSION['user']['id'])) {
			return false;
		}
		$loginpw = $this->gs($_SESSION['user']['id']);
		if (!isset($_SESSION['hash']) && !$this->check($loginpw, $_SESSION['hash'])) {
			session_destroy();
			return false;
		}
		$this->user = $_SESSION['user'];
		return true;
	}
	
	/**
	 * 设置一个账号为登录状态
	 * {@inheritDoc}
	 * @see \classlib\acl\acl_abstract::signin()
	 */
	public function signin($user, $loginpw, $expire=0, $cookie=true) {
		$_SESSION['user']= $user;
		$_SESSION['hash']= $this->crypt($loginpw);
		return session_id();
	}
	
	/**
	 * 请求cookie以及密钥资料
	 * {@inheritDoc}
	 * @see \classlib\acl\acl_abstract::signout()
	 */
	public function signout($clean=true) {
		if ($this->user) {//清理数据缓存信息
			$orm   = model::load($this->model)->set_mod($this->user['id']);
			$orm->get_orm()->clear()->table($orm->get_table())
				->where('userid', $this->user['id'])->where('sys', $this->sys)->delete();
			$skey = self::skey($this->user['id'], $this->ckey, $this->sys);
			model::$cacher->delete($skey);
		}
		if ($clean) {
			$_SESSION = null;
			session_destroy();
		}
	}
}